# -*- coding: utf-8 -*-
import scrapy
import pymysql
from bs4 import BeautifulSoup

from scrapy.http import Request
from scrapy.selector import Selector
from soccerResult.items import BifenGetItem
import re

class BifenGetSpider(scrapy.Spider):
    name = 'bifenGet'
    allowed_domains = ['live.500.com']

    #start_urls = ['http://live.500.com/wanchang.php?e=2019-04-08']

    start_urls = []
    # 读取请求的url列表
    file = open('urlList.txt', 'r', encoding='utf-8')
    urlList = file.readlines()
    for u in urlList:
        u = re.sub(r'\n', '', u)
        start_urls.append(u)

    # start_urls = ['http://live.500.com/wanchang.php?e=2014-10-08','http://live.500.com/wanchang.php?e=2015-10-08',
    #               'http://live.500.com/wanchang.php?e=2016-10-08','http://live.500.com/wanchang.php?e=2017-10-08',
    #               'http://live.500.com/wanchang.php?e=2018-10-08']

    def parse(self, response):
        items= []
        sel= Selector(response)

        baseInfo = sel.xpath('//table[@id=\"table_match\"]/tbody/tr').extract()
        itemId_info = sel.xpath('//table[@id=\"table_match\"]/tbody/tr/@id').extract()
        date_info = sel.xpath('//td[@class=\"left_wanchang\"]/table[1]/tr/td[2]/text()').extract()[0]
        # shuju_urls = sel.xpath('//table[@id=\"table_match\"]/tbody/tr/td[9]/a[1]/@href').extract()
        # yazhou_urls = sel.xpath('//table[@id=\"table_match\"]/tbody/tr/td[9]/a[2]/@href').extract()
        # ouzhou_urls = sel.xpath('//table[@id=\"table_match\"]/tbody/tr/td[9]/a[3]/@href').extract()
        # daxiao_urls =

        for i in range(0, len(itemId_info)):
            try:
                item = BifenGetItem()

                tmp = baseInfo[i]

                item['type'] = 'bifenGet'
                item['dateTime'] = date_info
                item['leagueName'] = Selector(text=baseInfo[i]).xpath('//tr/td[1]/a/text()').extract()[0]
                item['matchTime'] = Selector(text=baseInfo[i]).xpath('//tr/td[3]/text()').extract()[0]
                item['homeTeam'] = Selector(text=baseInfo[i]).xpath('//tr/td[5]/a/span/text()').extract()[0]
                item['guestTeam'] = Selector(text=baseInfo[i]).xpath('//tr/td[7]/a/span/text()').extract()[0]

                item['halfGoal'] = Selector(text=baseInfo[i]).xpath('//tr/td[8]/text()').extract()[0]
                item['homeGoal'] = Selector(text=baseInfo[i]).xpath('//tr/td[6]/div/a[1]/text()').extract()[0]
                item['guestGoal'] = Selector(text=baseInfo[i]).xpath('//tr/td[6]/div/a[3]/text()').extract()[0]
                item['fullGoal'] = item['homeGoal'] +' - '+ item['guestGoal']

                # "http://odds.500.com/fenxi/yazhi-653233.shtml"
                item['shuju_urls'] = 'http://odds.500.com/fenxi/'+Selector(text=baseInfo[i]).xpath('//tr/td[9]/a[1]/@href').extract()[0].split('/')[-1]
                item['yazhou_urls'] = 'http://odds.500.com/fenxi/'+Selector(text=baseInfo[i]).xpath('//tr/td[9]/a[2]/@href').extract()[0].split('/')[-1]
                item['ouzhou_urls'] = 'http://odds.500.com/fenxi/'+Selector(text=baseInfo[i]).xpath('//tr/td[9]/a[3]/@href').extract()[0].split('/')[-1]

                item['gameId'] = itemId_info[i][1:]
                item['daxiao_urls'] = 'http://odds.500.com/fenxi/daxiao-' + item['gameId'] + '.shtml'
                finalResult = 3
                if item['homeGoal'] > item['guestGoal']:
                    finalResult = 3
                elif item['homeGoal'] < item['guestGoal']:
                    finalResult = 0
                else:
                    finalResult = 1
                item['finalResult'] = finalResult

                items.append(item)

            except Exception as e:
                continue

        for item in items:
            yield Request(url=item['shuju_urls'], meta={'item_1': item}, callback=self.shuju_parse, dont_filter=True)


    def shuju_parse(self, response):
        sel = Selector(response)
        item = response.meta['item_1']
        # baseInfo = sel.xpath('//table[@id=\"table_match\"]/tbody/tr').extract()
        # sel.xpath('//div[@id=\"team_zhanji_1\"]/form/div[@class=\"M_content\"]').extract()
        # sel.xpath('//div[@id=\"team_zhanji_1\"]/form/div[@class=\"M_content\"]/table/tbody/tr[2]/td[5]/span/text()').extract()
        # sel.xpath('//div[@id=\"team_zhanji_1\"]/form/div[@class=\"M_content\"]/table/tbody/tr[2]').extract()
        # sel.xpath('//div[@id=\"team_zhanji_1\"]/form/div[@class=\"M_content\"]/table/tbody/tr[2]/td[6]/span/text()').extract()

        z1 = sel.xpath('//div[@id=\"team_zhanji_1\"]/form/div[@class=\"M_content\"]/table/tbody/tr[3]/td[6]/span/text()').extract()[0]
        z2 = sel.xpath('//div[@id=\"team_zhanji_1\"]/form/div[@class=\"M_content\"]/table/tbody/tr[4]/td[6]/span/text()').extract()[0]
        z3 = sel.xpath('//div[@id=\"team_zhanji_1\"]/form/div[@class=\"M_content\"]/table/tbody/tr[5]/td[6]/span/text()').extract()[0]
        z4 = sel.xpath('//div[@id=\"team_zhanji_1\"]/form/div[@class=\"M_content\"]/table/tbody/tr[6]/td[6]/span/text()').extract()[0]
        z5 = sel.xpath('//div[@id=\"team_zhanji_1\"]/form/div[@class=\"M_content\"]/table/tbody/tr[7]/td[6]/span/text()').extract()[0]
        z6 = sel.xpath('//div[@id=\"team_zhanji_1\"]/form/div[@class=\"M_content\"]/table/tbody/tr[8]/td[6]/span/text()').extract()[0]

        s1 = sel.xpath('//div[@id=\"team_zhanji_0\"]/form/div[@class=\"M_content\"]/table/tbody/tr[3]/td[6]/span/text()').extract()[0]
        s2 = sel.xpath('//div[@id=\"team_zhanji_0\"]/form/div[@class=\"M_content\"]/table/tbody/tr[4]/td[6]/span/text()').extract()[0]
        s3 = sel.xpath('//div[@id=\"team_zhanji_0\"]/form/div[@class=\"M_content\"]/table/tbody/tr[5]/td[6]/span/text()').extract()[0]
        s4 = sel.xpath('//div[@id=\"team_zhanji_0\"]/form/div[@class=\"M_content\"]/table/tbody/tr[6]/td[6]/span/text()').extract()[0]
        s5 = sel.xpath('//div[@id=\"team_zhanji_0\"]/form/div[@class=\"M_content\"]/table/tbody/tr[7]/td[6]/span/text()').extract()[0]
        s6 = sel.xpath('//div[@id=\"team_zhanji_0\"]/form/div[@class=\"M_content\"]/table/tbody/tr[8]/td[6]/span/text()').extract()[0]

        bs_home_zhanji = z1 + z2 + z3 + z4 + z5 + z6
        bs_guest_zhanji = s1 + s2 + s3 + s4 + s5 + s6

        bs_jiaozhan_text = sel.xpath('//span[@class=\"his_info\"]').extract()[0]

        bs = BeautifulSoup(bs_jiaozhan_text, 'html5lib')  # 'lxml'
        bs_jiaozhan = bs.find('span', class_='his_info')
        bs_jiaozhan = bs_jiaozhan.getText()
        item['bs_jiaozhan'] = bs_jiaozhan
        item['bs_home_zhanji'] = bs_home_zhanji
        item['bs_guest_zhanji'] = bs_guest_zhanji

        yield Request(url=item['ouzhou_urls'], meta={'item_3': item}, callback=self.ouzhou_parse, dont_filter=True)

    def ouzhou_parse(self, response):
        sel = Selector(response)
        item = response.meta['item_3']

        oddTemp = sel.xpath('//div[@id=\"table_btm\"]').extract()
        bs = BeautifulSoup(oddTemp[0], 'html5lib')

        item['avwinc2'] = bs.find('td', id='avwinc2').text
        item['avdrawc2'] = bs.find('td', id='avdrawc2').text
        item['avlostc2'] = bs.find('td', id='avlostc2').text

        item['avwinj2'] = bs.find('td', id='avwinj2').text
        item['avdrawj2'] = bs.find('td', id='avdrawj2').text
        item['avlostj2'] = bs.find('td', id='avlostj2').text

        yield Request(url=item['yazhou_urls'], meta={'item_3': item}, callback=self.yazhou_parse, dont_filter=True)

    def yazhou_parse(self, response):
        sel = Selector(response)
        item = response.meta['item_3']

        item['yazhouj'] = sel.xpath('//div[@id=\"table_btm\"]/table/tr[1]/td[3]/table/tbody/tr/td[2]/text()').extract()[0]
        item['yazhouc'] = sel.xpath('//div[@id=\"table_btm\"]/table/tr[1]/td[5]/table/tbody/tr/td[2]/text()').extract()[0]

        yield Request(url=item['daxiao_urls'], meta={'item_4': item}, callback=self.daxiao_parse, dont_filter=True)

    def daxiao_parse(self, response):
        sel = Selector(response)
        item = response.meta['item_4']

        item['daxiaoj'] = sel.xpath('//div[@id=\"table_btm\"]/table/tr[1]/td[3]/table/tbody/tr/td[2]/text()').extract()[0]
        item['daxiaoc'] = sel.xpath('//div[@id=\"table_btm\"]/table/tr[1]/td[5]/table/tbody/tr/td[2]/text()').extract()[0]

        yield item